using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using TileEngine;

namespace Bomberman
{
    public class Main : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

#if DEBUG
        FpsCounter fps;
#endif
        Menu menu;

        public Main()
        {
            this.graphics = new GraphicsDeviceManager(this);
            this.graphics.PreferredBackBufferWidth = 660;
            this.graphics.PreferredBackBufferHeight = 572;

            this.Content.RootDirectory = "Content";

            this.menu = new Menu(@"Menu\Default.xml", this);
            this.Components.Add(menu);

            this.Window.AllowUserResizing = false;
            ((System.Windows.Forms.Form)System.Windows.Forms.Form.FromHandle(this.Window.Handle)).Icon = new System.Drawing.Icon(this.Content.RootDirectory + @"\Bomberman.ico");
        }

        protected override void Initialize()
        {
            this.spriteBatch = new SpriteBatch(GraphicsDevice);
            this.Services.AddService(typeof(SpriteBatch), this.spriteBatch);

#if DEBUG
            fps = new FpsCounter(this);
            this.Components.Add(fps);

            this.IsFixedTimeStep = false;
            this.graphics.SynchronizeWithVerticalRetrace = false;
            this.graphics.ApplyChanges();
#endif

            base.Initialize();
        }

        protected override void LoadContent() { }

        protected override void UnloadContent() { }

        protected override void Update(GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            this.graphics.GraphicsDevice.Clear(Color.Black);

#if DEBUG
            this.Window.Title = this.menu.ScreenTitle + this.fps.FPS.ToString(" - FPS: 0");
#endif

            this.spriteBatch.Begin();
            base.Draw(gameTime);
            this.spriteBatch.End();
        }
    }
}
